package com.cargohu.aishouyu.member.controller;

import com.cargohu.aishouyu.common.CommonConstants;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.response.ApiResult;
import com.cargohu.aishouyu.member.dto.MemberAddDTO;
import com.cargohu.aishouyu.member.dto.MemberUpdateDTO;
import com.cargohu.aishouyu.member.req.MemberRequest;
import com.cargohu.aishouyu.member.resp.MemberDetailVo;
import com.cargohu.aishouyu.member.resp.MemberResponse;
import com.cargohu.aishouyu.member.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 会员表 控制器
 *
 * @author aaron.wang
 * @since 2023-11-27
 */
@Slf4j
@RestController
@RequestMapping(CommonConstants.API_ADMIN_PATH+"/member")
@Tag(name = "会员表")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 添加会员表
     *
     * @param memberAddDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @Operation(summary = "添加会员表")
    public ApiResult addMember(@Valid @RequestBody MemberAddDTO memberAddDTO) throws Exception {
        boolean flag = memberService.addMember(memberAddDTO);
        return ApiResult.result(flag);
    }

    /**
     * 修改会员表
     *
     * @param memberUpdateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @Operation(summary = "修改会员表")
    public ApiResult updateMember(@Valid @RequestBody MemberUpdateDTO memberUpdateDTO) throws Exception {
        boolean flag = memberService.updateMember(memberUpdateDTO);
        return ApiResult.result(flag);
    }

    /**
     * 删除会员表
     *
     * @param memberId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete/{memberId}")
    @Operation(summary = "删除会员表")
    public ApiResult deleteMember(@PathVariable Long memberId) throws Exception {
        boolean flag = memberService.deleteMember(memberId);
        return ApiResult.result(flag);
    }

    /**
     * 获取会员表详情
     *
     * @param memberId
     * @return
     * @throws Exception
     */
    @PostMapping("/get/{memberId}")
    @Operation(summary = "会员表详情")
    public ApiResult<MemberDetailVo> getMember(@PathVariable Long memberId) throws Exception {
        MemberDetailVo memberDetailVo = memberService.getMemberById(memberId);
        return ApiResult.success(memberDetailVo);
    }

    /**
     * 会员表分页列表
     *
     * @param memberRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    @Operation(summary = "会员表分页列表")
    public ApiResult<MemberResponse> getMemberList(@Valid @RequestBody MemberRequest memberRequest) throws Exception {
        Paging<MemberResponse> paging = memberService.getMemberList(memberRequest);
        return ApiResult.success(paging);
    }

}
